package LearnDataStructure.b_散列结构.HashMap;

import LearnDataStructure.b_散列结构.useNode;

import java.util.Iterator;

/**
 * @version: 1.0
 * @Author: wuqiu
 * @date: 2023-07-05 16:36
 * @description: LeetcodeLearnAlgorithm -> LearnDataStructure.b_散列结构.HashMap
 */
public class MyHashMapIterator<K, V> implements Iterator<useNode<K, V>> {
    private MyHashMap<K, V> source = null;
    private int i = 0;
    private useNode<K, V> p = null;

    public MyHashMapIterator(MyHashMap<K, V> source) {
        this.source = source;
        this.p = source.getBuckets()[0];
    }


    @Override
    public boolean hasNext() {
        while (i < source.getCapacity() && p == null) {//如果碰到了空坑
            i++;//查看下一个坑
            if (i == source.getCapacity()) {//越过了最后一个坑
                p = null;//已经查看了所有的元素了
            } else {
                p = source.getBuckets()[i];
            }
        }
        return p != null;
    }

    @Override
    public useNode<K, V> next() {
        useNode<K, V> res = p;
        p = p.next;
        return res;
    }
}
